<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%><!DOCTYPE HTML>
<html>
<head>
<title>用户管理</title>
<script type="text/javascript">

	var isAdd = true;
	$.extend($.fn.validatebox.defaults.rules, {  
		typepassword : {
			validator : function(value, param) {
				return value == $('#pform input[name="password"]').val();
			},
			message : "您两次输入的密码不一致"
		}
	});
	$(function() {
		$('#valid').onoff({name:'valid'});
		$('#account').validatebox({required:true,delay:300,validType:'remote["checkUsername","username"]',invalidMessage:'该账号已经存在'});
		$('#toolbar').toolbar({buttons:[
		    {name:'search',iconCls:'icon-search',text:'检索'},
           	{name:'add',iconCls:'icon-add',text:'创建'},
           	{name:'add2',iconCls:'icon-add',text:'创建2'},
       		{name:'edit',iconCls:'icon-edit',text:'修改',disabled:true},
       		{name:'del',iconCls:'icon-del',text:'删除',disabled:true},
       		{name:'pswd',iconCls:'icon-pswd',text:'修改密码',disabled:true},
       		{name:'grant',iconCls:'icon-tablechk',text:'分配角色',disabled:true}
      	],handler:handler});
		$('#grid').datagrid({url: 'page',title:'用户列表',pagination:true,singleSelect:true,rownumbers:true,striped:true,fitColumns:true,
			columns:[[
			{field:'username',title:'用户名',width:120},
			{field:'name',title:'姓名',width:120},
			{field:'valid',title:'状态',width:40,formatter:$g.bv}
			]]
			,onClickRow:function(i,r) {
				$('#toolbar').toolbar('enableAll'); //启动所有按钮
			}
			,onBeforeLoad:function(param) {
				$('#toolbar').toolbar('search,add,add2');
			}
		});
		
	});

	function handler() {
		if(this.name == 'search') return toSearch();
		if(this.name == 'add') return toAdd();
		if(this.name == 'add2') return toAdd2();
		var item = $('#grid').datagrid('getSelected');
		if(!item) return;
		switch(this.name) {
		case 'edit': toEdit(item);break;
		case 'del': toDel(item);break;
		case 'pswd': toPswd(item);break;
		case 'grant': toGrant(item);break;
		}
	}
	
	function toAdd2(){
		console.log(222)
	}
	
	function toSearch() {
		$('#grid').datagrid('load',$('#qform').serializeArray());
	}
	
	function toAdd() {
		isAdd = true;
		$('#account').validatebox('enableValidation');
		showDialog({gender:'1',valid:true,dcFlag:'Y'});
	}

	function toEdit(item) {
		show({code:'admin_user_index',title:'编辑用户信息',iconCls:'icon-user',uri:'edit?id='+item.id,width:800,height:600,
			options : {
				success : function(data) { 
					$('#grid').datagrid('reload'); 
				}
			}
		});
		
		return;
		$.post('fetch/'+item.id,function(data) {
			isAdd = false;
			$('#account').validatebox('disableValidation');
			showDialog(data);
		},'json');
	}
	
	function toDel(item) {
		$confirm('您确定要删除“'+item.username+'”用户信息吗？',function() {
			$.post('delete/?id='+item.id,function(r) {
				if(r.code <= 0) return $error(r.msg);
				$('#grid').datagrid('reload');
				$prompt('删除成功');
			},'json');
		});
	}

	function showDialog(data) {
		$('#dataform').form('clear').form('load',data);
		$('#gender').radiobox('setValue',data.gender);
		$('#valid').onoff('setValue',data.valid);
		$('#dcFlag').radiobox('setValue',data.dcFlag);
		$('#cancelCa').linkbutton(data.caNo?'enable':'disable');
		var oa = $('#account');
		if(data.id) {
			oa.addClass('readonly');
			oa[0].readOnly = true;
			$('#passwordMemo').hide();
		}
		else  {
			oa.removeClass('readonly');
			oa[0].readOnly = false;
			$('#passwordMemo').show();
		}
		$('#dialog').dialog('open');
	}
	
	function doSave() {
		$.submit('#dataform',function(r) {
			if(r.code<0) return $alert(r.msg);
			$('#dialog').dialog('close');
			$('#grid').datagrid('reload');
			$prompt('保存成功');
		});
	}

	function toPswd(item) {
		$('#pswd_id').val(item.id);
		$('#dialogPswd').dialog('open');
	}
	
	function updatePswd() {
		$.submit('#pform',function(r) {
			if(r.code<0) return $alert(r.msg);
			$('#dialogPswd').dialog('close');
			$prompt('保存成功');
		});
	}

	function toGrant(item) {
		if(!$('#roles').attr('data-id')) {
			$('#roles').attr('data-id',item.id).datalist({data:[],striped:true,singleSelect:false,checkOnSelect:true,checkbox:true,lines:true,valueField:'id',textField:'name'});
		}
		$('#roles').attr('data-id',item.id);
		$.post('roles',{id:item.id,type:item.unitType},function(data) {
			var roles = data.roles;
			var rids = data.rids;
			if(!roles) return $alert('该用户所在的机构没有可选择的岗位');
			var grid = $('#roles').datalist('loadData',roles).datalist('uncheckAll');
			if(rids&&rids.length>0) {
				for(var i = 0; i < rids.length; i++) {
					for(var j = 0; j < roles.length; j++) {
						if(rids[i] == roles[j].id) {
							grid.datalist('checkRow',j);
							break;
						}
					}
				}
			}
			$('#dialogGrant').dialog('open');
		},'json');
	}

	function grant() {
		var params = [];
		var rows = $('#roles').datalist('getChecked');
		for(var i = 0; i < rows.length; i++) params[i] = {name:'roles',value:rows[i].id};
		params[params.length] = {name:'id',value:$('#roles').attr('data-id')};
		$.post('grant',params,function(r) {
			if(r.code<0) return $alert(a.msg);
			$prompt('授予成功！');
			$('#grid').datagrid('reload');
			$('#dialogGrant').dialog('close');
		},'json');
	}

	/**
	 * 判断账号是否存在
	 */
	function checkAccount(){
		if(!isAdd) return;
		alert($account.val());
		$.ajax({
			async:false,
			url:'checkUsername',
			data:{username:$account.val()},
			type:'POST',
			dataType:'json',
			success:function(data){
				if(data.code <0){
				
					top.$alert("账号：" + $account.val() + " 已经存在!");
					$account.val('');
				}
			}
		});
	}
</script>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:90px;">
		<div id="toolbar" class="toolbar"></div>
		<form id="qform">
			<table class="form-tbl">
				<tr class="form-tr">
					<th class="form-th">租户名称：</th>
					<td class="form-td" style="width: 130px" >
						<input type="text" name="name" class="form-text" style="width: 120px" />
					</td>
					<td></td>
					<!-- <th class="form-th">状态：</th>
					<td class="form-td">
						<input type="text" name="unitName" class="form-text" style="width: 120px" />
					</td> -->
				</tr>
			</table>
		</form>
	</div>
	<div data-options="region:'center',border:false">
		<table id="grid"></table>
		
		<div id="dialog" class="easyui-dialog" data-options="title:'用户信息',iconCls:'icon-user',closed:true,modal:true,width:600,height:400,top:30,buttons:[{iconCls:'icon-save',text:'保存',handler:doSave}]">
			<form id="dataform" action="save.do" method="post">
				<input type="hidden" name="id"/>
				<table class="form-tbl">
					<tr class="form-tr">
						<th class="form-th">登录帐号：</th>
						<td class="form-td required">
							<input type="text" id="account" name="username" class="easyui-validatebox form-text" style="width:180px;" data-options="required:true" onblur="checkAccount()"/>
							<span class="form-memo" id="passwordMemo">默认密码：12345</span>
						</td>
					</tr>
					<tr class="form-tr">
						<th class="form-th">姓　　名：</th>
						<td class="form-td required">
							<input type="text" name="name" class="easyui-validatebox form-text" style="width:180px;" data-options="required:true" />
						</td>
					</tr>
					
					<tr class="form-tr">
						<th class="form-th">状　　态：</th>
						<td class="form-td required">
							<div id="valid"></div>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="dialogPswd" class="easyui-dialog" data-options="title:'修改密码',iconCls:'icon-pswd',closed:true,modal:true,width:500,height:220,top:30,buttons:[{iconCls:'icon-save',text:'保存新密码',handler:updatePswd}]">
			<form id="pform" action="updatePassword.do" method="post">
				<input type="hidden" id="pswd_id" name="id"/>
				<table class="form-tbl">
					<tr class="form-tr">
						<th class="form-th">新 密 码：</th>
						<td class="form-td required">
							<input type="password" name="password" class="easyui-validatebox form-text" data-options="required:true"/>
						</td>
					</tr>
					<tr class="form-tr">
						<th class="form-th">密码确认：</th>
						<td class="form-td required">
							<input type="password" name="typepassword" class="easyui-validatebox form-text" data-options="required:true,validType:'typepassword'" />
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="dialogGrant" class="easyui-dialog" data-options="title:'分配角色',iconCls:'icon-post',closed:true,modal:true,width:400,height:300,top:30,buttons:[{iconCls:'icon-save',text:'确定授予',handler:grant}]">
			<div id="roles"></div>
		</div>
	</div>
</body>
</html>